/**
 * Entity.java Create on 2012-7-31     
 *      
 * Copyright (c) 2012-7-31 by yiming     
 *      
 * @author <a href="alenzhangym@hotmail.com">Yiming</a>     
 * @version 1.0 
 */

package com.yimi.service;

import android.os.Parcel;
import android.os.Parcelable;
import android.util.Log;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * @author Yiming
 * @version 1.0
 */
public class Entity implements Parcelable {
    private String TAG = "Entity";
    private String type;

    Entity() {
        this.setType(this.getClass().getName());
    }
  
    private static Entity readParcel(Parcel in) {
        String className = in.readString();
        Class cl = null;
        try {
            cl = Class.forName(className);
        } catch (ClassNotFoundException e) {

        }
        if (cl == null)
            return null;
        Object o = null;
        try {
            o = cl.newInstance();
        } catch (InstantiationException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        } catch (IllegalAccessException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        Method method = null;
        try {
            method = cl.getMethod("readToParcel", new Class[] {
                Parcel.class
            });
        } catch (SecurityException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        try {
            method.invoke(o, in);
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        if (o == null)
            return null;
        return (Entity) o;
    }

    /**
     * @param in
     */
    public Entity(Parcel in) {
        this.readFromParcel(in);
    }

    public void readFromParcel(Parcel in) {
        Log.e(this.getClass().getName(), "Start to read");
        this.type = in.readString();
        Log.e(this.getClass().getName(), "read over");
    }

    /**
     * 该方法是以读了类型，
     * 
     * @param in
     */
    public void readToParcel(Parcel in) {

    }

    /*
     * (non-Javadoc)
     * @see android.os.Parcelable#describeContents()
     */
    @Override
    public int describeContents() {
        return 0;
    }

    /*
     * (non-Javadoc)
     * @see android.os.Parcelable#writeToParcel(android.os.Parcel, int)
     */
    @Override
    public void writeToParcel(Parcel parcel, int arg1) {
        Log.e("Entity", "start to write type is " + type);
        parcel.writeString(type);
        this.writePacel(parcel, arg1);
        Log.e("Entity", "write over");
    }

    public void writePacel(Parcel arg0, int arg1) {

    }

    /**
     * @return the type
     */
    public String getType() {
        return type;
    }

    /**
     * @param type the type to set
     */
    public void setType(String type) {
        this.type = type;
    }

    public static final Parcelable.Creator<Entity> CREATOR = new Parcelable.Creator<Entity>() {
        @Override
        public Entity createFromParcel(Parcel in) {
            // String type = in.readString();
            // if(type.equals(BaseImageItem.class.getName())){
            // return new BaseImageItem(in);
            // }else if(type.equals(ImageList.class.getName())){
            // return new ImageList(in);
            // }
            // return new Entity(in);
            Log.e("ENTITY CREATOR", " create start ");
            return readParcel(in);
        }

        @Override
        public Entity[] newArray(int size) {
            return new Entity[size];
        }
    };

    public String toString() {
        return "[" + this.getType() + "]";
    }
}
